package com.baizhi.cmfz.controller;

import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.entity.Banner;
import com.baizhi.cmfz.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@RequestMapping("/banner")
@Controller
public class BannerController {

    @Autowired
    private BannerService bannerService;
    @RequestMapping("/showPage")
    @ResponseBody
    public Map showPage(int page, int limit){
        return  bannerService.showPageBanner(page,limit);
    }
    @RequestMapping("/insertBanner")
    @ResponseBody
    public Map insertBanner(Banner banner ,MultipartFile file, HttpServletRequest request) throws IOException {
        //设置上传路径
        String realPath=request.getServletContext().getRealPath("/upload");
        //新文件重命名
        String newFile=new Date().getTime()+"_"+file.getOriginalFilename();
        file.transferTo(new File(realPath+"/"+newFile));
        banner.setPic("/upload/"+newFile);
        return  bannerService.insertBanner(banner);
    }
    @RequestMapping("/deleteBanner")
    @ResponseBody
    public Map deleteBanner(int id){
        return  bannerService.deleteBanner(id);
    }
    @RequestMapping("/showOne")
    @ResponseBody
    public Banner showOne(int id){
        return  bannerService.showOne(id);
    }
    @RequestMapping("/updateBanner")
    @ResponseBody
    public Map updateBanner(Banner banner ,MultipartFile file,HttpServletRequest request) throws IOException {
        //获取文件上传的连接
        String realPath=request.getServletContext().getRealPath("upload");
        //文件重命名
        String newFile=new Date().getTime()+"_"+file.getOriginalFilename();
        file.transferTo(new File(realPath+"/"+newFile));
        return  bannerService.updateBanner(banner);
    }
}
